﻿using Dapper;
using System;
using BCEntity.Common.Case.WebsiteCase;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using BCCommon;

namespace BCData.Common.Case.WebsiteCase
{
    /// <summary>
    /// 官网案例
    /// </summary>
    public class WebsiteCaseData : IWebsiteCaseData
    {
        private readonly IDatabaseContext databaseContext;
        private const string QUERY_EXISTS = @"SELECT 1 FROM websitecase";
        private const string QUERY = @"SELECT WebsiteCaseId,CreateAdminId,CreateAdminName,CreateTime,UpdateTime,EditAdminId,EditAdminName,CaseName,Summary,ShowLocation,Icon,Background,LinkUrl,Enabled,ViewTimes FROM websitecase";
        public WebsiteCaseData(IDatabaseContext databaseContext)
        {
            this.databaseContext = databaseContext;
        }
        #region 检查官网案例是否存在
        public async Task<bool> Exists(int websiteCaseId)
        {
            string sql = string.Format(@"{0} WHERE WebsiteCaseId=?WebsiteCaseId", QUERY_EXISTS);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WebsiteCaseId", websiteCaseId, DbType.Int32);
            return await this.databaseContext.Slave.ExecuteScalarAsync(sql, parameters) != null;
        }
        #endregion

        #region 添加官网案例
        public async Task<WebsiteCaseEntity> Add(WebsiteCaseEntity entity)
        {
            string sql = SqlCommands.GetInsertSql("websitecase", new string[] { "CreateAdminId", "CreateAdminName", "CaseName", "ShowLocation", "Summary", "Icon", "Background", "LinkUrl" });
            sql = string.Format("{0} {1} WHERE WebsiteCaseId=LAST_INSERT_ID();", sql, QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?CreateAdminId", entity.CreateAdminId, DbType.Int64, size: 20);
            parameters.Add("?CreateAdminName", entity.CreateAdminName?.Trim(), DbType.AnsiString, size: 20);
            parameters.Add("?CaseName", entity.CaseName?.Trim(), DbType.AnsiString, size: 20);
            parameters.Add("?Summary", entity.Summary?.Trim(), DbType.String);
            parameters.Add("?ShowLocation", entity.ShowLocation?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?Icon", entity.Icon?.Trim(), DbType.AnsiString, size: 1000);
            parameters.Add("?Background", entity.Background?.Trim(), DbType.AnsiString, size: 1000);
            parameters.Add("?LinkUrl", entity.LinkUrl?.Trim(), DbType.AnsiString, size: 1000);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<WebsiteCaseEntity>(sql, parameters, databaseContext.Transaction);

        }
        #endregion

        #region 修改官网案例
        public async Task<WebsiteCaseEntity> Update(WebsiteCaseEntity entity)
        {
            string sql = SqlCommands.GetUpdateSql("websitecase", new string[] { "EditAdminId", "EditAdminName", "CaseName", "Summary", "ShowLocation", "Icon", "Background", "LinkUrl" }, new string[] { "WebsiteCaseId" });
            sql = string.Format("{0} {1} WHERE WebsiteCaseId=?WebsiteCaseId;", sql, QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WebsiteCaseId", entity.WebsiteCaseId, DbType.Int32, size: 11);
            parameters.Add("?EditAdminId", entity.EditAdminId, DbType.Int64, size: 20);
            parameters.Add("?EditAdminName", entity.EditAdminName?.Trim(), DbType.AnsiString, size: 20);
            parameters.Add("?CaseName", entity.CaseName?.Trim(), DbType.AnsiString, size: 20);
            parameters.Add("?Summary", entity.Summary?.Trim(), DbType.String);
            parameters.Add("?ShowLocation", entity.ShowLocation?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?Icon", entity.Icon?.Trim(), DbType.AnsiString, size: 1000);
            parameters.Add("?Background", entity.Background?.Trim(), DbType.AnsiString, size: 1000);
            parameters.Add("?LinkUrl", entity.LinkUrl?.Trim(), DbType.AnsiString, size: 1000);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<WebsiteCaseEntity>(sql, parameters, databaseContext.Transaction);
        }
        #endregion

        #region 删除官网案例
        public async Task<bool> Delete(int websiteCaseId)
        {
            string sql = @"DELETE FROM websitecase WHERE WebsiteCaseId=?WebsiteCaseId";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WebsiteCaseId", websiteCaseId, DbType.Int32);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
        }
        #endregion

        #region 获取官网案例
        public async Task<WebsiteCaseEntity> GetEntity(int websiteCaseId)
        {
            string sql = string.Format(@"{0} WHERE WebsiteCaseId=?WebsiteCaseId", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WebsiteCaseId", websiteCaseId, DbType.Int64);
            return await this.databaseContext.Slave.QueryFirstOrDefaultAsync<WebsiteCaseEntity>(sql, parameters);
        }
        #endregion

        #region 获取官网案例列表
        public async Task<IEnumerable<WebsiteCaseEntity>> GetList()
        {
            string sql = string.Format(@"{0} WHERE Enabled=1", QUERY);
            return await this.databaseContext.Slave.QueryAsync<WebsiteCaseEntity>(sql);
        }
        #endregion

        #region 获取官网案例分页
        public async Task<(IEnumerable<WebsiteCaseEntity>, int)> Query(bool? enabled, DateTime? start, DateTime? end, string sortName, bool? ascending, int pageIndex, int pageSize)
        {
            List<string> wheres = new List<string>();
            DynamicParameters parameters = new DynamicParameters();
            if (enabled.HasValue)
            {
                wheres.Add("Enabled=?Enabled");
                parameters.Add("?Enabled", enabled.Value, DbType.Boolean);
            }
            if (start.HasValue && !end.HasValue)
            {
                wheres.Add("CreateTime>=?Start");
                parameters.Add("?Start", start.Value, DbType.DateTime);
            }
            else if (!start.HasValue && end.HasValue)
            {
                wheres.Add("CreateTime<=?End");
                parameters.Add("?End", end.Value, DbType.DateTime);
            }
            else if (start.HasValue && end.HasValue)
            {
                wheres.Add("CreateTime>=?Start AND CreateTime<=?End");
                parameters.Add("?Start", start.Value, DbType.DateTime);
                parameters.Add("?End", end.Value, DbType.DateTime);
            }
            string sql = string.Format(@"{0} {1} ORDER BY {4} {5} LIMIT {2},{3}; SELECT COUNT(1) FROM websitecase {1}",
                  QUERY,
                  wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "",
                  (pageIndex == 0 ? 0 : pageIndex - 1) * pageSize,
                  pageSize,
                  string.IsNullOrWhiteSpace(sortName) ? "CreateTime" : sortName,
                  ascending.HasValue && ascending.Value ? "ASC" : "DESC");
            return await this.databaseContext.Slave.QueryMultipleAsync<WebsiteCaseEntity, int>(sql, parameters);
        }
        #endregion

        #region 启用官网案例
        public async Task<bool> Enabled(int websiteCaseId)
        {
            string sql = @"UPDATE websitecase SET Enabled=1 WHERE WebsiteCaseId=?WebsiteCaseId";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WebsiteCaseId", websiteCaseId, DbType.Int32);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
        }
        #endregion

        #region 禁用官网案例
        public async Task<bool> Disabled(int websiteCaseId)
        {
            string sql = @"UPDATE websitecase SET Enabled=0 WHERE WebsiteCaseId=?WebsiteCaseId";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WebsiteCaseId", websiteCaseId, DbType.Int32);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
        }
        #endregion
    }
}



